//////////////////////////////////////////////////
// glTexture2D.cpp
//
// Author: Tarik Belabbas et Adrien Hoffer
//////////////////////////////////////////////////

#include <QImage>
#include <QGLWidget>

#include "glTexture2D.h"
#include "../common/Common.h"

int	glTexture2D::m_loadCount = 0;

//------------------------------------------------------------------------------
glTexture2D::glTexture2D()
{
	init();
}

//------------------------------------------------------------------------------
glTexture2D::glTexture2D( const char* _texturePath )
{
	init();

	setTexturePath( _texturePath );
}

//------------------------------------------------------------------------------
glTexture2D::~glTexture2D()
{
	unload();
}

//------------------------------------------------------------------------------
void glTexture2D::init()
{
	m_texture	= 0;
	m_isLoaded	= false;
}

//------------------------------------------------------------------------------
void glTexture2D::setTexturePath( const char* _texturePath )
{
	if ( _texturePath )
	{
		m_texturePath = _texturePath;
	}
}

//------------------------------------------------------------------------------
void glTexture2D::setTextureName( const char* _textureName )
{
	if ( _textureName )
	{
		m_texturePath = TEXTURE_PATH;
		m_texturePath += _textureName;
	}
}

//------------------------------------------------------------------------------
void glTexture2D::loadTexture()
{
	if ( !m_isLoaded && !m_texturePath.empty() )
	{
		QImage rawTexture;
		if ( rawTexture.load( m_texturePath.c_str() ) )
		{
			rawTexture = QGLWidget::convertToGLFormat( rawTexture );

			glGenTextures( 1, &m_texture );
			glBindTexture( GL_TEXTURE_2D, m_texture );

			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
			glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

			//ST_LOG( ( "%dx%d, glTexture.width(), glTexture.height() ) );
			glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, rawTexture.width(),
					rawTexture.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, rawTexture.bits() );

			//glTexImage2D( GL_TEXTURE_2D, 0, GL_COMPRESSED_RGB_ARB, rawTexture.width(),
			//		rawTexture.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, rawTexture.bits() );

			m_isLoaded = true;
			ST_LOG( ( "Texture load count = %d: %s", ++m_loadCount, m_texturePath.c_str() ) );
		}
	}
}

//------------------------------------------------------------------------------
void glTexture2D::loadTexturePath( const char* _texturePath )
{
	if ( !m_isLoaded && _texturePath )
	{
		m_texturePath = _texturePath;

		loadTexture();
	}
}

//------------------------------------------------------------------------------
void glTexture2D::loadTextureName( const char* _textureName )
{
	if ( !m_isLoaded && _textureName )
	{
		m_texturePath = TEXTURE_PATH;
		m_texturePath += _textureName;

		loadTexture();
	}
}

//------------------------------------------------------------------------------
void glTexture2D::unload()
{
	if ( ( m_texture != 0 ) && m_isLoaded )
	{
		glDeleteTextures( 1, &m_texture );

		m_texture	= 0;
		m_texturePath.clear();
		m_isLoaded	= false;
	}
}

//------------------------------------------------------------------------------
void glTexture2D::bindTexture()
{
	if ( m_isLoaded )
	{
		glBindTexture( GL_TEXTURE_2D, m_texture );
	}
}

////------------------------------------------------------------------------------
//const glTexture2D& glTexture2D::operator=( const glTexture2D& _texture )
//{
//	if ( m_isLoaded )
//	{
//		unload();
//	}
//
//	m_texture			= _texture.m_texture;
//	m_texturePath		= _texture.m_texturePath;
//
//	return *this;
//}
